<template>
  <div class="login_container">
    <div class="login_box">
      <h2 class="login_title">欢迎登录</h2>
      <p class="login_description">请输入您的用户名和密码</p>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="rules"
        label-width="80px"
        class="login_form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item class="btn_group">
          <el-button type="primary" :loading="loading" @click="onSubmit">
            登陆
          </el-button>
          <el-button @click="onCancel">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>


<script>
export default {
  name: "login",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
        ],
      },
      loading: false,
    };
  },
  methods: {
     onSubmit() {
       this.$refs.loginForm.validate().then(() => {
         this.loading = true;
         this.$http.post("auth/login", this.loginForm).then(response => {
             this.loading = false;
             window.sessionStorage.setItem("token", response.data.token);
             this.$router.push("/home");
           }
         ).catch(() => {
           this.loading = false;
         })
       }).catch(() => {
         this.loading = false;``
       });
     },
    onCancel() {
      this.$refs.loginForm.resetFields();
    },
  },
};
</script>



<style lang="less" scoped>
.login_container {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #5a87f7, #6ed3cf);
}

.login_box {
  width: 100%;
  max-width: 360px;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
  background: #fff;
  text-align: center;
}

.login_title {
  font-size: 24px;
  color: #333;
  margin-bottom: 8px;
}

.login_description {
  font-size: 14px;
  color: #666;
  margin-bottom: 20px;
}

.login_form {
  text-align: left;
}

.el-input {
  border-radius: 6px;
  transition: border-color 0.3s;
}

.el-input:hover {
  border-color: #409eff;
}

.btn_group {
  text-align: right;
}

.el-button {
  border-radius: 6px;
  padding: 8px 20px;
  transition: all 0.3s;
}

.el-button.primary {
  background-color: #409eff;
  border-color: #409eff;
}

.el-button.primary:hover {
  background-color: #66b1ff;
}

@media (max-width: 768px) {
  .login_box {
    padding: 20px;
  }
  .login_title {
    font-size: 20px;
  }
}
</style>


